package com.adam.example;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TransportClientHolder {

    private static TransportClientHolder INSTANCE = null;
    private TransportClient transportClient;

    private TransportClientHolder() throws UnknownHostException {
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch")
                .build();
        transportClient = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.0.111"), 9300));
    }

    public static TransportClientHolder getInstance() throws UnknownHostException {
        if(INSTANCE == null) {
            synchronized (TransportClientHolder.class) {
                if(INSTANCE == null) {
                    INSTANCE = new TransportClientHolder();
                }
            }
        }
        return INSTANCE;
    }

    public TransportClient getTransportClient() {
        return transportClient;
    }

}
